package com.ccjq.jq167.servlet;

import com.ccjq.jq167.po.StudentPo;
import com.ccjq.jq167.service.impl.service_impl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StudentServlet")
public class StudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String str=request.getParameter("action");
        if(str.equals("tianjia")){
            StudentPo u=new StudentPo();
            String name=request.getParameter("name");
            String sex=request.getParameter("sex");
            String age=request.getParameter("age");
            String address=request.getParameter("address");
            String school=request.getParameter("school");
            u.setName(name);
            u.setSex(sex);
            u.setAge(age);
            u.setAddress(address);
            u.setSchool(school);
            service_impl s=new service_impl();
            s.insert(u);
            List<StudentPo> list=s.select();
            request.setAttribute("list",list);
            request.getRequestDispatcher("/WEB-INF/jsp/ZhuYe.jsp").forward(request,response);
        }
        if(str.equals("shanchu")){
            StudentPo u=new StudentPo();
            String uid=request.getParameter("id");

            System.out.print("____________"+uid);
            u.setId(uid);
            service_impl s=new service_impl();
            s.delete(u);
            List<StudentPo> list=s.select();
            request.setAttribute("list",list);
            request.getRequestDispatcher("/WEB-INF/jsp/ZhuYe.jsp").forward(request,response);
        }

        if(str.equals("xiugai")){
            StudentPo u=new StudentPo();
            String id=request.getParameter("id1");
            u.setId(id);
            service_impl s=new service_impl();
            StudentPo u1=s.chaxunzzz(u);
            request.setAttribute("Student",u1);
            request.getRequestDispatcher("/WEB-INF/jsp/Update.jsp").forward(request,response);
        }
        if(str.equals("update")){
            StudentPo u=new StudentPo();
            String id=request.getParameter("id");
            String name=request.getParameter("name");
            String sex=request.getParameter("sex");
            String age=request.getParameter("age");
            String address=request.getParameter("address");
            String school=request.getParameter("school");
            u.setId(id);
            u.setName(name);
            u.setSex(sex);
            u.setAge(age);
            u.setAddress(address);
            u.setSchool(school);
            service_impl s=new service_impl();
            s.update(u);
            List<StudentPo> list=s.select();
            request.setAttribute("list",list);
            request.getRequestDispatcher("/WEB-INF/jsp/ZhuYe.jsp").forward(request,response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
